﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Navigation;
using Milkify.Data;
using Milkify.Interfaces;

namespace Milkify.Views
{
    /// <summary>
    /// Interaction logic for TaskEditControl.xaml
    /// </summary>
    public partial class TaskEditControl : PageFunction<bool>, ITaskEditView
    {
        public TaskEditControl()
        {
            InitializeComponent();
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            CompleteTaskEdit();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            CancelTaskEdit();
        }

        private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void CompleteTaskEdit()
        {
            //TODO: find a way to do this via DataBinding?
            TaskEditData editData = ViewData as TaskEditData;            
            string formattedTags = TextTags.Text;
            editData.SetTaskTags(formattedTags, new char[] { ',', ';' });

            // Notify that the task edit is complete.
            if (EditComplete != null)
                EditComplete(this, EventArgs.Empty);

            OnReturn(new ReturnEventArgs<bool>(true));
        }

        private void CancelTaskEdit()
        {
            // Notify that the task edit has been cancelled.
            if (EditCancelled != null)
                EditCancelled(this, EventArgs.Empty);

            OnReturn(null);
        }

        #region IView Members

        public object ViewData
        {
            get { return DataContext; }
            set
            {
                if (value != DataContext)
                {
                    DataContext = value;
                    OnViewDataChanged();
                }
            }
        }

        private void OnViewDataChanged()
        {
            if (DataContext != null)
            {
                TaskEditData editData = (TaskEditData)DataContext;
                //TODO: find a way to do this via DataBinding?
                TextTags.Text = editData.GetTags(", ");
            }
        }

        #endregion

        #region ITaskEditView Members

        public event EventHandler EditComplete;
        public event EventHandler EditCancelled;

        public void FocusNameControl()
        {
            TextName.Focus();
        }

        public void FocusDueDateControl()
        {
            TextDue.Focus();
        }

        public void FocusRepeatControl()
        {
            TextRepeat.Focus();
        }

        public void FocusEstimateControl()
        {
            TextEstimate.Focus();
        }

        public void FocusTagsControl()
        {
            TextTags.Focus();
        }

        public void FocusUrlControl()
        {
            TextUrl.Focus();
        }

        public void FocusLocationControl()
        {
            ComboLocation.Focus();
            ComboLocation.IsDropDownOpen = true;
        }

        public void FocusPriorityControl()
        {
            ComboPriority.Focus();
            ComboPriority.IsDropDownOpen = true;
        }

        public void SuggestName(string name)
        {
            TextName.Text = name;
        }

        public void SuggestTaskList(IronCow.TaskList taskList)
        {
            ComboList.SelectedValue = taskList;
        }

        public void SuggestPriority(IronCow.TaskPriority priority)
        {
            ComboPriority.SelectedValue = priority;
        }

        public void SuggestDueDate(string due)
        {
            TextDue.Text = due;
        }

        public void SuggestRepeat(string recurrence)
        {
            TextRepeat.Text = recurrence;
        }

        public void SuggestEstimate(string estimate)
        {
            TextEstimate.Text = estimate;
        }

        public void SuggestTags(string tags)
        {
            TextTags.Text = tags;
        }

        public void SuggestUrl(string url)
        {
            TextUrl.Text = url;
        }

        public void SuggestLocation(IronCow.Location location)
        {
            ComboLocation.SelectedValue = location;
        }

        #endregion
    }
}
